package com.xxl.job.executor.service.jobhandler;

import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;
import com.xxl.job.core.log.XxlJobLogger;
import com.xxl.job.executor.crawler.job.AllProductDetail;
import com.xxl.job.executor.crawler.util.JedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;


/**
 * @Author lcx
 * @Date 2018/11/29 15:50
 * 启动爬虫
 */
@JobHandler(value = "start_crawler")
@Component
public class Crawler extends IJobHandler {
    @Autowired
    AllProductDetail allProductDetail;

    @Override
    public ReturnT<String> execute(String param) {
        XxlJobLogger.log("启动爬虫！");
        Jedis jedis = JedisUtil.getJedis();
        String type = param.substring(param.indexOf(",")+1);
        String num = param.substring(0, param.indexOf(","));
        allProductDetail.start_crawler(type);

        if (jedis.lrange("detailUrl", 0, 1).size() == 0) {
            return SUCCESS;
        } else if(Integer.valueOf(num) == 0) {
            return SUCCESS;
        }

        XxlJobLogger.log("爬虫结束！");
        
        return execute(String.valueOf(Integer.valueOf(num)- 1)+","+type);
    }
}
